﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Input;
using CommunityToolkit.Mvvm.ComponentModel;
using CommunityToolkit.Mvvm.Input;
using ShopingList.Dal;
using ShopingList.DBModel;
using ShopingList.Model;

namespace ShopingList.ViewMode
{
    /// <summary>
    /// 主页视图
    /// </summary>
    public class MainPageModel : ObservableObject
    {
        /// <summary>
        /// 
        /// </summary>
        private ObservableCollection<MainPageModelItem> _listRight;
        /// <summary>
        /// 
        /// </summary>
        public ObservableCollection<MainPageModelItem> listRight { get => _listRight; set { _listRight = value; OnPropertyChanged(); } }
        /// <summary>
        /// 
        /// </summary>
        private ObservableCollection<MainPageModelItem> _listLeft;
        /// <summary>
        /// 
        /// </summary>
        public ObservableCollection<MainPageModelItem> listLeft { get => _listLeft; set { _listLeft = value; OnPropertyChanged(); } }

        /// <summary>
        /// 
        /// </summary>
        shopListDal splDal { get; set; }
        /// <summary>
        /// 
        /// </summary>
        public ICommand LaodData { get; set; }
        /// <summary>
        /// 
        /// </summary>
        public MainPageModel()
        {
            splDal = new();
            listRight = new ObservableCollection<MainPageModelItem>();
            listLeft = new ObservableCollection<MainPageModelItem>();
            LaodData = new RelayCommand(LaodDataMethod);
            IniData();
        }

        private async void LaodDataMethod()
        {
            listLeft.Clear();
            listRight.Clear();
            await IniData();
        }

        /// <summary>
        /// 数据初始化
        /// </summary>
        private async Task<int> IniData()
        {
            var dbdata = await splDal.SelectAll();
            if (dbdata.Count == 0)
            {
                BaseData baseData = new BaseData();
                await splDal.AddData(baseData.shopLists);
                dbdata = await splDal.SelectAll();
            }
            dbdata = dbdata.OrderBy(x => x.index).ToList();
            var gdata = dbdata.GroupBy(x => x.spType);
            var d1 = new ObservableCollection<MainPageModelItem>();
            var d2 = new ObservableCollection<MainPageModelItem>();
            foreach (var item in gdata)
            {
                d1.Add(new MainPageModelItem(item.First()));
                foreach (var item2 in item)
                {
                    d2.Add(new MainPageModelItem(item2));
                }
            }
            listLeft = d1;
            listRight = d2;
            return 0;
        }

        public async void Delete(MainPageModelItem item)
        {

            await splDal.helpClass.Database.DeleteAsync(item.Getdb());
            await IniData();
        }
    }

    public class MainPageModelItem : ObservableObject
    {
        /// <summary>
        /// 
        /// </summary>
        public ICommand Click { get; set; }
        public MainPageModelItem(shopList shopList)
        {
            spCode = shopList.spCode;
            spType = shopList.spType;
            spName = shopList.spName;
            guid = shopList.guid;
            index = shopList.index;
            Click = new RelayCommand(ClickMethod);
        }
        public shopList Getdb()
        {
            return new shopList()
            {
                guid = guid
            };
        }
        /// <summary>
        /// 
        /// </summary>
        private bool _isClick;
        /// <summary>
        /// 
        /// </summary>
        public bool isClick { get => _isClick; set { _isClick = value; OnPropertyChanged(); } }

        private void ClickMethod()
        {

        }

        /// <summary>
        /// 
        /// </summary>
        private string _spName;
        /// <summary>
        /// 
        /// </summary>
        public string spName { get => _spName; set { _spName = value; OnPropertyChanged(); } }
        /// <summary>
        /// 
        /// </summary>
        private string _spType;
        /// <summary>
        /// 
        /// </summary>
        public string spType { get => _spType; set { _spType = value; OnPropertyChanged(); } }

        /// <summary>
        /// 
        /// </summary>
        private string _spCode;
        /// <summary>
        /// 
        /// </summary>
        public string spCode { get => _spCode; set { _spCode = value; OnPropertyChanged(); } }


        /// <summary>
        /// 
        /// </summary>
        private string _guid;
        /// <summary>
        /// 
        /// </summary>
        public string guid { get => _guid; set { _guid = value; OnPropertyChanged(); } }


        /// <summary>
        /// 
        /// </summary>
        private int _index;
        /// <summary>
        /// 
        /// </summary>
        public int index { get => _index; set { _index = value; OnPropertyChanged(); } }

    }
}
